package com.library.web.borrow;

import com.library.model.Borrow;
import com.library.service.BorrowService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/borrow")
public class BorrowServlet extends HttpServlet {
    // 日期格式化工具（处理页面日期字符串转Date）
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private final BorrowService borrowService = new BorrowService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if (action == null || "list".equals(action)) {
            List<Borrow> borrows = borrowService.findByCondition(
                    request.getParameter("username"),
                    request.getParameter("bookTitle"),
                    request.getParameter("status"));
            request.setAttribute("borrows", borrows);
            request.getRequestDispatcher("borrow/borrow.jsp").forward(request, response);
        } else if ("delete".equals(action)) {
            borrowService.deleteById(Integer.parseInt(request.getParameter("id")));
            response.sendRedirect("borrow?action=list");
        } else if ("return".equals(action)) {
            try {
                borrowService.backBookById(Integer.parseInt(request.getParameter("id")));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("borrow?action=list");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            try {
                Borrow borrow = new Borrow();
                borrow.setUsername(request.getParameter("username"));
                borrow.setBookTitle(request.getParameter("bookTitle"));
                borrow.setIsbn(request.getParameter("isbn"));
                borrow.setFinePerDay(Double.parseDouble(request.getParameter("finePerDay")));
                borrow.setBorrowDate(sdf.parse(request.getParameter("borrowDate")));
                borrow.setExpectReturnDate(sdf.parse(request.getParameter("expectReturnDate")));
                borrow.setStatus("UNRETURNED");
                borrowService.addBorrow(borrow);
            } catch (ParseException | NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("borrow?action=list");
        } else if ("update".equals(action)) {
            try {
                Borrow borrow = new Borrow();
                borrow.setId(Integer.parseInt(request.getParameter("id")));
                borrow.setUsername(request.getParameter("username"));
                borrow.setBookTitle(request.getParameter("bookTitle"));
                borrow.setIsbn(request.getParameter("isbn"));
                borrow.setFinePerDay(Double.parseDouble(request.getParameter("finePerDay")));
                borrow.setBorrowDate(sdf.parse(request.getParameter("borrowDate")));
                borrow.setExpectReturnDate(sdf.parse(request.getParameter("expectReturnDate")));
                borrow.setStatus(request.getParameter("status"));
                borrowService.update(borrow);
            } catch (ParseException | NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("borrow?action=list");
        }
    }
}
